/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javacommunity.model.dao.impl;

import com.javacommunity.model.dao.UserDAO;
import com.javacommunity.model.entity.User;
import com.javacommunity.hibernate.HibernateUtil;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Pham
 */
public class UserDAOImpl implements UserDAO {

    private static UserDAOImpl userDAO;

    public static UserDAOImpl getInstance() {
        if (userDAO == null) {
            userDAO = new UserDAOImpl();
        }
        return userDAO;
    }
    private Session session;
    private Transaction ts;

    @Override
    public User getUserById(int userId) {
        User user = null;
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            user = (User) session.get(User.class, userId);
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
        }finally{
            session.close();
        }
        return user;
    }

    @Override
    public User getUserByUserName(String userName) {
        session = HibernateUtil.getSessionFactory().openSession();
        User user = null;
        try {
            ts = session.beginTransaction();
            String sql = "FROM User as u WHERE u.userName = ?";
            Query query = session.createQuery(sql);
            query.setParameter(0, userName);
            user = (User) query.uniqueResult();
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
        }finally{
            session.close();
        }
        return user;
    }

    @SuppressWarnings("unchecked")
	@Override
    public List<User> getUsers(int param) {
        session = HibernateUtil.getSessionFactory().openSession();
        List<User> users = null;
        try {
            ts = session.beginTransaction();
            Query query = session.createSQLQuery("CALL sp_GetUsers(?)").addEntity(User.class).setParameter(0, param);
            users = query.list();
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
        }finally{
            session.close();
        }
        return users;
    }

    @Override
    public boolean createUser(User user) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.save(user);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean updateUser(User user) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.update(user);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean deleteUser(User user) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.delete(user);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean removeUser(User user) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            String sql = "UPDATE User set active = false WHERE userId = ?";
            Query query = session.createQuery(sql);
            query.setParameter(0, user.getUserId());
            query.executeUpdate();
            ts.commit();
            return true;
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean restoreUser(User user) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            String sql = "UPDATE User set active = true WHERE userId = ?";
            Query query = session.createQuery(sql);
            query.setParameter(0, user.getUserId());
            query.executeUpdate();
            ts.commit();
            return true;
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean checkUserName(String userName) {
        boolean isCheck = false;

        session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            String sql = "FROM User as u WHERE u.userName = ?";
            Query query = session.createQuery(sql);
            query.setParameter(0, userName);
            if (query.list() != null && query.list().size() > 0) {
                isCheck = true;
            }
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
            isCheck = false;
        } finally {
            session.close();
        }
        return isCheck;
    }

    @Override
    public boolean checkEmail(String email) {
        boolean isCheck = false;

        session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            String sql = "FROM User as u WHERE u.email = ?";
            Query query = session.createQuery(sql);
            query.setParameter(0, email);
            if (query.list().size() > 0) {
                isCheck = true;
            }
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
            isCheck = false;
        } finally {
            session.close();
        }
        return isCheck;
    }

    @Override
    public boolean checkLogin(String userName, String pwd) {
        session = HibernateUtil.getSessionFactory().openSession();
        boolean isCheck = false;
        try {
            ts = session.beginTransaction();
            String sql = "FROM User u WHERE u.userName = 0 and u.pwd = 1";
            Query query = session.createQuery(sql);
            query.setParameter(0, userName);
            query.setParameter(1, pwd);
            if (query.list().size() > 0) {
                isCheck = true;
            }
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            isCheck = false;
        }finally{
            session.close();
        }
        return isCheck;
    }

}
